/**
 * Copyright (c) 2021 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.stat.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.stat.dto.StatChannelConfDTO;
import io.renren.modules.stat.service.StatChannelConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 栏目配置管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("stat/channelConf")
@Api(tags = "栏目配置管理")
public class StatChannelConfController {
    @Autowired
    private StatChannelConfService statChannelConfService;

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("stat:channelConf:info")
    public Result<StatChannelConfDTO> get(@PathVariable("id") Long id) {
        StatChannelConfDTO data = statChannelConfService.get(id);

        return new Result<StatChannelConfDTO>().ok(data);
    }

    @GetMapping("getByChannel/{channelId}")
    @ApiOperation("信息")
    @RequiresPermissions("stat:channel:conf")
    public Result<StatChannelConfDTO> getByChannel(@PathVariable("channelId") Long channelId) {
        StatChannelConfDTO data = statChannelConfService.getByChannel(channelId);

        return new Result<StatChannelConfDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("stat:channel:conf")
    public Result save(@RequestBody StatChannelConfDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        statChannelConfService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("stat:channel:conf")
    public Result update(@RequestBody StatChannelConfDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        statChannelConfService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("stat:channel:conf")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        statChannelConfService.delete(ids);

        return new Result();
    }

}